﻿KISSY.add("fgt/ui/confirm", function (S, Overlay) {
    function Confirm(config) {
        this._init(config || {});
    }

    S.augment(Confirm, {
        _init: function (config) {
            var self = this;

            self.dialog = new Overlay.Dialog({
                prefixCls: "fgt-",
                footerContent: "<a class='btn btn1 cancel-btn'></a>"
                                + "<a class='btn confirm-btn'></a>",
                elStyle: {
                    position: S.UA.ie == 6 ? "absolute" : "fixed"
                },
                elCls: "fgt-confirm",
                closable: false,
                align: {
                    points: ['cc', 'cc']
                },
                width: 520,
                mask: true,
                draggable: false,
                zIndex: 999999
            });
            self.isRender = false;
        },

        _bindEvent: function () {
            var self = this;

            self.confirmBtn.on("click", function (event) {
                event.halt();
                self.dialog.hide();
                var callback = self.comfirmCallback;
                if (callback) {
                    callback.apply(self.callbackContext || self, self.confirmCallBackParams || []);
                }
            });

            self.cancelBtn.on("click", function (event) {
                event.halt();
                self.dialog.hide();
            });
        },

        _render: function () {
            var self = this,
                dialog = self.dialog,
                contentEl;

            dialog.render();
            contentEl = dialog.get("contentEl");
            self.header = contentEl.all(".fgt-stdmod-header");
            self.body = contentEl.all(".fgt-stdmod-body");
            self.confirmBtn = contentEl.all(".confirm-btn");
            self.cancelBtn = contentEl.all(".cancel-btn");

            self._bindEvent();
            self.isRender = true;
        },

        show: function (config) {
            var self = this;

            if (!self.isRender) {
                self._render();
            }
            self.comfirmCallback = config.confirmCallback;
            self.callbackContext = config.callbackContext;
            self.confirmCallBackParams = config.confirmCallBackParams;
            self.header.text(config.header || "");
            self.body.text(config.body || "");
            self.confirmBtn.text(config.confirmBtnText || "");
            self.cancelBtn.text(config.cancelBtnText || "");
            self.dialog.show();
        },

        hide: function () {
            this.dialog.hide();
        }
    });

    return Confirm;
}, {
    requires: ["overlay"]
});